<?php
/**
 * 所有的确认付款功能.
 * User: madong
 * Date: 2018/10/16
 * Time: 12:18 AM
 */

namespace app\api\controller\jiaoyi;
use think\Controller;
use think\Db;
use think\Request;

class Querenfukuan extends Controller
{

    /**
     * 付款方法
     *
     * 参数
     *
     * @id  当前订单ID
     *
     * @uordernumber  当前订单号
     *
     * @oordernumber  对方订单号
     *
     * @fukuan_type  支付方式类型 0 / 1
     *
     * @zhifu  支付方式
     */
    public function Order_Querenfukuan(){

        $res = Request::instance();

        if (!$res->param('zhifu')){echo json_encode(['zt'=>0,'msg'=>'请先付款']);die();}

        if (!$res->isPost()){echo json_encode(['zt'=>0,'msg'=>'Methods Error']);die();}

        $User_all = Db::name('jiaoyibuylog')->where('ordernumber',$res->param('uordernumber'))->find();//当前订单号所有信息

        if ($User_all['oorderzt'] == 1){echo json_encode(['zt'=>0,'msg'=>'您确认过了']);die();}

        if ($res->param('fukuan_type') == 1){

            $order_data = [

                'buyname' => $User_all['uname'],

                'sellname' => $User_all['oname'],

                'id' =>$res->param('id'),

                'uordernumber' => $res->param('uordernumber'),

                'oordernumber' => $res->param('oordernumber'),

                'zhifu' => $res->param('zhifu')

            ];

            $this->Order_Querenfukuan_one($order_data);

        }elseif ($res->param('fukuan_type') == 0){

            $order_data = [

                'buyname' => $User_all['uname'],

                'sellname' => $User_all['oname'],

                'id' =>$res->param('id'),

                'uordernumber' => $res->param('uordernumber'),//当前订单号

                'oordernumber' => $res->param('oordernumber'),//对方订单号

                'zhifu' => $res->param('zhifu')

            ];

            $this->Order_Querenfukuan_two($order_data);

        }else{

            if (Db::name('eyiip')->insert([

                'ip'=>\request()->ip(),

                'note'=>'错误传参类型fukuan_type - 文件路径/jiaoyi/Querenfukuan 方法名：Order_Querenfukuan',

                'time'=>time()

            ])){

                echo json_decode(['zt'=>1,'msg'=>'(^_^)']);

            }else{

                echo json_decode(['zt'=>0,'msg'=>'Error']);

            }

        }

    }

    //第一种确认付款方式

    protected function Order_Querenfukuan_one($order_data){

        Db::startTrans();

        try{

            Db::name('jiaoyibuylog')->where('id',$order_data['id'])->where('ordernumber',$order_data['uordernumber'])->update([

                'oorderzt' => 1,

                'ofukuanfs' => $order_data['zhifu'],

                'fktime' => time()

            ]);

            Db::name('jiaoyi')->where('ordernumber',$order_data['oordernumber'])->setInc('yfkdd',1);

            Db::name('jiaoyi')->where('ordernumber',$order_data['oordernumber'])->setDec('wfkdd',1);

            Db::commit();

            echo json_encode(['zt'=>1,'msg'=>'操作成功,等待对方释放','data'=>1]);

        } catch (\Exception $e) {

            echo json_encode(['zt'=>0,'msg'=>'确认付款失败!']);

            Db::rollback();
        }

    }

    //第二种确认付款方式

    protected function Order_Querenfukuan_two($order_data){

        Db::startTrans();

        try{

            Db::name('jiaoyibuylog')->where('id',$order_data['id'])->where('ordernumber',$order_data['ordernumber'])->update([

                'oorderzt' => 1,

                'ofukuanfs' => $order_data['zhifu'],

                'fktime' => time()

            ]);

            Db::name('jiaoyi')->where('ordernumber',$order_data['oordernumber'])->setInc('yfkdd',1);

            Db::name('jiaoyi')->where('ordernumber',$order_data['oordernumber'])->setDec('wfkdd',1);

            echo json_encode(['zt'=>1,'msg'=>'操作成功,等待对方确认','data'=>1]);

            Db::commit();

        } catch (\Exception $e) {

            echo json_encode(['zt'=>0,'msg'=>'未能查询到此订单!']);

            Db::rollback();
        }

    }
}